/* eslint-disable no-unused-vars */
/*
 * @Author: AHANG
 * @Date: 2022-04-12 14:33:56
 * @LastEditors: AHANG
 * @LastEditTime: 2022-07-20 15:14:44
 * @Description:
 */
//userApi.js
var models = require("../db");
var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var $sql = require("../sqlMap");

// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();
var jsonWrite = function (res, ret) {
  if (typeof ret === "undefined") {
    res.json({
      code: "1",
      msg: "操作失败",
    });
  } else {
    res.json(ret);
  }
};
//登录
// router.post("/login", (req, res) => {
//   console.log(req.body);
//   let sql = $sql.admin_user.login;
//   const user = req.body.user;
//   const pass = req.body.pass;
//   conn.query(sql, [user, pass], function (err, result) {
//     if (result.length > 0) {
//       jsonWrite(res, {
//         msg: "登录成功",
//         data: result,
//         NO: true,
//       });
//     } else {
//       jsonWrite(res, {
//         msg: "账号或密码错误",
//         NO: false,
//       });
//       console.log(err);
//     }
//   });
// });

//查
router.get("/searchUser", (req, res) => {
  console.log(req.body);
  let sql = $sql.admin_user.search;
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, {
        msg: "查询成功",
        data: result,
      });
    }
  });
});

//删
router.delete("/deleteUser", (req, res) => {
  console.log(req.body);
  let sql = $sql.admin_user.delete;
  const UId = req.body.id;
  conn.query(sql, [UId], function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, {
        msg: "删除成功",
        data: result,
      });
    }
  });
});

// 改;
router.post("/updateUser", (req, res) => {
  console.log(req.body);
  [userName, userSex, userMz, userId, userPhone, userQs, userAddress] = [
    req.body.name,
    req.body.sex,
    req.body.mz,
    req.body.id,
    req.body.phone,
    req.body.qs,
    req.body.address,
  ];
  let sql = $sql.admin_user.update;
  conn.query(
    sql,
    [userName, userSex, userMz, userId, userPhone, userQs, userAddress, userId],
    function (err, result) {
      if (err) {
        console.log(err);
      }
      if (result) {
        jsonWrite(res, {
          msg: "修改成功",
          data: result,
        });
      }
    }
  );
});
// 增
router.post("/addUser", (req, res) => {
  console.log(req.body);
  // const bookId = req.body.id;
  [userName, userSex, userMz, userId, userPhone, userQs, userAddress] = [
    req.body.name,
    req.body.sex,
    req.body.mz,
    req.body.id,
    req.body.phone,
    req.body.qs,
    req.body.address,
  ];
  let sql = $sql.admin_user.insert;
  conn.query(
    sql,
    [userName, userSex, userMz, userId, userPhone, userQs, userAddress],
    function (err, result) {
      if (err) {
        jsonWrite(res, {
          msg: "请检查填写是否符合或书名冲突",
          NO: false,
        });
        console.log(err);
      }
      if (result) {
        jsonWrite(res, {
          msg: "增加成功",
          data: result,
          NO: true,
        });
      }
    }
  );
});
module.exports = router;
